/*
 *  Copyright 2009 Advanced Industrial Science and Technology (AIST).
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*! \file
 *  \brief
 */
#pragma once

// thrust, includes
#include <thrust/device_ptr.h>
#include <thrust/device_delete.h>

#include <thrust/pending/limits.hpp>

// stl, includes
#include <iostream>

namespace thrust {

namespace detail {

  template <typename Directed, typename Vertex>
  struct edge_base
  {
    __host__ __device__
    inline edge_base()
      : m_source(thrust::numeric_limits<Vertex>::max())
      , m_target(thrust::numeric_limits<Vertex>::max()) {}
    
    __host__ __device__
    inline edge_base( Vertex s, Vertex d)
      : m_source(s), m_target(d) {}
    
    //
    // XXX
    //
    volatile Vertex m_source;
    volatile Vertex m_target;
  };
  
  template <typename Directed, typename Vertex, typename EdgeProperty>
  class edge_desc_impl : public edge_base<Directed,Vertex>
  {

    // Type Deffinition
  private:
    typedef edge_desc_impl self_t;
    typedef edge_base<Directed,Vertex> super_t;
    
  public:
    typedef Directed directed_category;
    typedef EdgeProperty property_type;
    typedef Vertex vertex_type;
    
    // Internal Variables
  public:
    device_ptr<property_type> m_property;
    
  public:
    __host__ __device__
    inline edge_desc_impl() 
      : super_t(), m_property(thrust::device_pointer_cast<property_type>(0))
    {}
    
    __host__ __device__
    inline edge_desc_impl( Vertex s, Vertex d)
      : super_t(s,d), m_property(thrust::device_pointer_cast<property_type>(0)) 
    {}
    
    __host__ __device__
    inline edge_desc_impl( Vertex s, Vertex d, const device_ptr<property_type>& pro)
      : super_t(s,d), m_property(pro) 
    {}

    __host__ __device__
    inline edge_desc_impl( const edge_desc_impl<Directed,Vertex,EdgeProperty>& e)
      : super_t(e.m_source,e.m_target), m_property(e.m_property)
    {}

    __host__ __device__
    inline device_ptr<property_type>& get_property() { return m_property; }
    
    __host__ __device__
    inline const device_ptr<property_type>& get_property() const { return m_property; }
  
    __host__ __device__
    inline void
    swap()
    {
      Vertex tmp =this->m_source;
      this->m_source =this->m_target;
      this->m_target =tmp;
    }

    __host__ __device__
    inline void
    set_property( const device_ptr<property_type>& property)
    {
      m_property =property;
    }

    __host__
    inline void 
    clear_property()
    {
      cudaFree( m_property.get());
      m_property =thrust::device_pointer_cast<property_type>(0);
    }
  };
  
  
  template <typename EdgeDescriptor, typename EdgeProperty>
  struct set_edge_property
  {
    template <typename T>
    __device__ 
    inline void 
    operator()( const T& value)
    {
      EdgeDescriptor& e =get<0>( value);
      EdgeProperty& p =get<1>( value);
      //e.set_property( p);
      e.m_property =thrust::device_pointer_cast<EdgeProperty>(&p);
    }
    
  };
  
  template <typename Edge>
  struct clear_edge {
    __device__
    void operator()( Edge& e){
      e.m_source =thrust::numeric_limits<typename Edge::vertex_type>::max();
      e.m_target =thrust::numeric_limits<typename Edge::vertex_type>::max();      
    }
  };
  
  template <typename Edge>
  struct equal_edge : public binary_function<Edge,Edge,bool>
  {
    typedef typename Edge::directed_category directed_category;

    __host__ __device__
    bool operator()( const Edge& x,  const Edge& y) const
    {
      return operator()( x, y, directed_category());
    }
    
    __host__ __device__
    bool operator()( const Edge& x, const Edge& y, directed_tag) const
    {
      return (x.m_source ==y.m_source && x.m_target ==y.m_target);
    }
    
    __host__ __device__
    bool operator()( const Edge& x, const Edge& y, undirected_tag) const
    {
      return (x.m_source ==y.m_source && x.m_target ==y.m_target) ?
	true : ( (x.m_source ==y.m_target && x.m_target ==y.m_source) ? true : false);
    }
  };
  
  template <typename Edge>
  struct equal_edge_source : public binary_function<Edge,Edge,bool>
  {
    typedef typename Edge::directed_category directed_category;
    
    __host__ __device__
    bool operator()( const Edge& x, const Edge& y) const
    {
      return operator()( x, y, directed_category());
    }
    
    __host__ __device__
    bool operator()( const Edge& x, const Edge& y, directed_tag) const
    {
      return ( x.m_source ==y.m_source);
    }
    
    __host__ __device__
    bool operator()( const Edge& x, const Edge& y, undirected_tag) const
    {
      return ( x.m_source ==y.m_source) ?
	true : ( ( x.m_source ==y.m_target) ? true : false);
    }
  };
  
  template <typename Edge>
  struct equal_edge_target : public binary_function<Edge,Edge,bool>
  {
    typedef typename Edge::directed_category directed_category;
    
    __host__ __device__
    bool operator()( const Edge& x, const Edge& y) const
    {
      return operator()( x, y, directed_category());
    }
    
    __host__ __device__
    bool operator()( const Edge&x , const Edge& y, directed_tag) const
    {
      return ( x.m_target ==y.m_target);
    }
    
    __host__ __device__
    bool operator()( const Edge& x, const Edge& y, undirected_tag) const
    {
      return ( x.m_source ==y.source) ? 
	true : ( ( x.m_source ==y.m_target) ? true : false);
    }
  };
  
  template <typename Edge>
  struct less_edge_source : public binary_function<Edge,Edge,bool>
  {
    __host__ __device__
    bool operator()( const Edge& x, const Edge& y)
    {
      return ( x.m_source ==y.m_source) ? 
	( x.m_target <y.m_target ? true : false) : ( x.m_source <y.m_source ? true : false);
    }
  };
  
  template <typename Edge>
  struct less_edge_target : public binary_function<Edge,Edge,bool>
  {
    __host__ __device__
    bool operator()( const Edge& x, const Edge& y)
    {
      return ( x.m_target ==y.m_target) ? 
	( x.m_source <y.m_source ? true : false) : ( x.m_target <y.m_target ? true : false);
    }
  };
  
} // end namespace detail

template <typename Directed, typename Vertex, typename EdgeProperty>
std::ostream&
operator<<( std::ostream& os, 
            const thrust::detail::edge_desc_impl<Directed,Vertex,EdgeProperty>& e)
{
  std::string str;
  
  if( detail::is_directed( Directed()))
    str ="->";
  else
    str ="-";
  
  return os <<"(" <<e.m_source
            <<str <<e.m_target 
#ifdef DEBUG
            <<",[" <<e.get_property()
            <<"]"
#endif
            <<")";
}

} // end namespace thrust
